<?php

class ThemeSettings {

    protected $css_path;
    protected $js_path;

    public function ThemeSettings() {
        
    }

    public function initialize($js_path, $css_path) {
        global $pagenow;
        $this->css_path = $css_path;
        $this->js_path = $js_path;
        add_action('wp_print_scripts', array(&$this, 'print_jquery_script'), 8);
        add_action('admin_menu', array(&$this, 'add_to_admin_menu'));
        if ('themes.php' == $pagenow && isset($_GET['page']) && ($_GET['page'] == basename(__FILE__))) {
            add_action('admin_print_scripts', array(&$this, 'ipost_media_upload_scripts'));
            add_action('admin_print_styles', array(&$this, 'ipost_media_upload_styles'));
        }
    }

    protected function print_jquery_script() {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3');
    }

    /* Admin scripts + ajax jquery code */

    protected function ipost_panel_admin_js() {
        wp_enqueue_script('ipost_functions_init', IPOST_THEME_JS_URL . 'functions-init.js');
        wp_enqueue_script('ipost_colorpicker', IPOST_THEME_JS_URL . 'colorpicker.js');
        wp_enqueue_script('ipost_colorpicker_eye', IPOST_THEME_JS_URL . 'eye.js');
        wp_enqueue_script('ipost_colorpicker_utils', IPOST_THEME_JS_URL . 'utils.js');
        wp_enqueue_script('ipost_colorpicker_layout', IPOST_THEME_JS_URL . 'layout.js');
        wp_enqueue_script('ipost_functions_bootstrap', IPOST_THEME_JS_URL . 'bootstrap.min.js');
    }

    /* --------------------------------------------- */

    /* Adds additional ipost css */

    protected function ipost_panel_css_admin() {
        ?> 
        <link rel="stylesheet" href="<?php echo IPOST_THEME_CSS_URL ?>layout.css" type="text/css" />
        <link rel="stylesheet" href="<?php echo IPOST_THEME_CSS_URL ?>colorpicker.css" type="text/css" />
        <link rel="stylesheet" href="<?php echo IPOST_THEME_CSS_URL ?>ipost-admin.css" type="text/css" />
        <link rel="stylesheet" href="<?php echo IPOST_THEME_CSS_URL ?>bootstrap.css" type="text/css" />
        <style type="text/css">
        </style>
        <!--[if IE 7]>
        <style type="text/css">			
        </style>
        <![endif]-->  
        <!--[if IE 8]>
        </style>
        <![endif]-->  
    <?php
    }

    /* --------------------------------------------- */

    protected function ipost_media_upload_scripts() {
        wp_enqueue_script('media-upload');
        wp_enqueue_script('thickbox');
        //wp_register_script('my-upload', IPOST_THEME_JS_URL.'custom_uploader.js', array('jquery','media-upload','thickbox'));
        //wp_enqueue_script('my-upload');
    }

    protected function ipost_media_upload_styles() {
        wp_enqueue_style('thickbox');
    }

    protected function add_to_admin_menu() {

        global $themename;
        $epanel = basename(__FILE__);

        if (isset($_GET['page']) && $_GET['page'] == $epanel && isset($_REQUEST['action'])) {
            //epanel_save_data( 'js_disabled' ); //saves data when javascript is disabled
        }

        $core_page = add_theme_page($themename . " Options", "Theme Options", 'switch_themes', basename(__FILE__), array(&$this, 'render_setting_form'));

        add_action("admin_print_scripts-{$core_page}", array(&$this, 'ipost_panel_admin_js'));

        add_action("admin_head-{$core_page}", array(&$this, 'ipost_panel_css_admin'));
    }

    protected function render_setting_form() {
        global $themename, $ipt_options;
        if (isset($_REQUEST['saved'])) {
            if ($_REQUEST['saved'])
                echo '<div id="message" class="updated fade"><p><strong>' . $themename . ' settings saved.</strong></p></div>';
        };
        if (isset($_REQUEST['reset'])) {
            if ($_REQUEST['reset'])
                echo '<div id="message" class="updated fade"><p><strong>' . $themename . ' settings reset.</strong></p></div>';
        };
        ?>        
        <div id="admin_main_pane">            
            <div class="admin-panel-ctl row">
                <div id="of_nav_pane">
                    <ul class="list-option-menu">
                        <li class="logo">SETTINGS</li>
                        <?php
                        foreach ($ipt_options as $option_name => $option) {
                            ?>
                            <li name="left-menu-nav" content-id="<?php echo $option_name ?>" class="<?php echo $option_name ?>-nav <?php echo $option['icon'] ?>">
                                <a title="Home Settings"> <?php echo $option['name'] ?></a>
                            </li> 
                            <li class="seperator"></li>
            <?php
        }
        ?>
                    </ul>
                </div>
                <div id="of_content_pane">
                    <div class="toolbar"> 
                        <img style="display:none" src="<?php echo IPOST_THEME_IMG_URL ?>/images/loading-bottom.gif" class="ajax-loading-img ajax-loading-img-bottom" alt="Working...">
                        <input type="hidden" id="security" name="security">	
                        <button id="of_save_top" type="button" class="btn btn-primary">Save All Changes</button>
                    </div>

                    <form class="forms">
                                <?php
                                foreach ($ipt_options as $option_name => $option) {
                                    ?>
                            <div id="<?php echo $option_name ?>-content" class="left-content" style="display:none">
                                <ul>
                                    <?php
                                    foreach ($option['options'] as $option_value => $option_data) {
                                        $type = $option_data['type'];
                                        $name = $option_data['name'];
                                        $id = $option_data['id'];
                                        $desc = $option_data['desc'];
                                        $std = $option_data['std'];
                                        if ($type == 'text') {
                                            ?>
                                            <li>
                                                <label for="<?php echo $id ?>" class="bold"><?php echo $name ?></label>
                                                <input type="text" placeholder="Type <?php echo $name ?>…" id="<?php echo $id ?>" name="<?php echo $id ?>" value="<?php echo $std ?>" class="seven input-gray" size="60"/>
                                                <div class="help-block"><?php echo $desc ?></div>
                                            </li>   
                                            <?php
                                        } else if ($type == 'upload') {
                                            ?>
                                            <li>
                                                <label for="<?php echo $id ?>" class="bold"><?php echo $name ?></label>                                     
                                                <input type="file" id="<?php echo $id ?>" name="<?php echo $id ?>"/>
                                                <div class="help-block"><?php echo $desc ?></div>
                                            </li>   
                                    <?php
                                }
                            }
                            ?>
                                </ul>
                            </div>
            <?php
        }
        ?>                    
                    </form>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="toolbar bottom"> 
                <img style="display:none" src="<?php echo IPOST_THEME_IMG_URL ?>/images/loading-bottom.gif" class="ajax-loading-img ajax-loading-img-bottom" alt="Working...">
                <input type="hidden" id="security" name="security">	
                <button id="of_save_bottom" type="button" class="btn btn-primary">Save All Changes</button>
            </div>

        </div>
        <?php
    }

}
?>
